package util.ir;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.store.LockObtainFailedException;

import util.delicious.DmozEntryDescription;
import util.io.FileInput;
import util.nlp.HashDictionary;
import util.preprocessing.dmozExtractor;

public class Experiments {

	public static void genDmozIndexData(String dmoz, String tags) {

		int values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
				17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 };
		HashDictionary tags_dictionary = new HashDictionary(tags, "", "\t", 0,
				values);

		Hashtable<String, DmozEntryDescription> hash = dmozExtractor
				.getURLandInfo(dmoz, null);
	

		int j=0;

		Enumeration<String> keys = hash.keys();
		while (keys.hasMoreElements()) {

			DmozEntryDescription entry = hash.get(keys.nextElement());


			String tag_concat = "";

			LinkedList<String> tag = tags_dictionary.getEntries(entry.url);
			
			
			if (tag != null ) {
		
				for (int i = 0; i < tag.size(); i++) {
		
					tag_concat = tag_concat + tag.get(i) + " ";
				}

			}

			System.out.println(entry.url + "\t"
					+ entry.title.replaceAll("\\s+", " ") + "\t"
					+ entry.description.replaceAll("\\s+", " ") + "\t"
					+ entry.topic + "\t" + tag_concat);

		}
		System.out.println(j);

	}
	
	
	public static void indexDmoz(String file, String indexPath) throws CorruptIndexException,
			LockObtainFailedException, IOException {
		String field_names[] = { util.ir.Constants.URL,
				util.ir.Constants.DMOZ_TITLE,
				util.ir.Constants.DMOZ_DESCRIPTION,
				util.ir.Constants.DMOZ_CATEGORY, util.ir.Constants.TAGS };

		String combined_names[] = { util.ir.Constants.DMOZ_TITLE_DESCRIPTION };

		int fields_combined[][] = { { 1, 2 } };

		Indexer.indexer(file, field_names, fields_combined, combined_names,
				indexPath, new StandardAnalyzer());

	}

	
	
	public static void queryDmoz(String query, String indexPath) throws CorruptIndexException, ParseException, IOException{
		Hashtable<String, Float> hash = new 		Hashtable<String, Float>();
		
		QueryIndex searcher = new QueryIndex();
		searcher.indexSearch(query, new StandardAnalyzer(), util.ir.Constants.DMOZ_TITLE_DESCRIPTION,
				util.ir.Constants.TAGS, indexPath,
				 hash,100); 
		
		
	
		Enumeration<String> keys = hash.keys();
		
		while(keys.hasMoreElements()){
			
			System.out.println(keys.nextElement());
			
		}
		
		
	}

	public static void main(String args[]) {

		String path = "/media/sata_/data/delicious/delicious_dmoz_kids_dict.txt";
		path = "/home/sergio/projects/data/dmoz/kt/delicious_dmoz_kids_simple.txt";
		String dmoz = "/home/sergio/projects/data/dmoz/kt/kt-content.rdf.u8";
		
		 path = "/media/sata_/data/delicious/delicious_dmoz_all_dictA.txt";
		dmoz = "/home/sergio/projects/data/dmoz/content.rdf.u8";

		
	//	genDmozIndexData(dmoz,path);
		
		// indexDmoz(dmoz, path);

		String dmoz_index_data = "/media/sata_/data/dmoz/dmoz_extended_kids.txt";
		
		/*try {
			
			indexDmoz(dmoz_index_data, util.ir.Constants.DMOZ_INDEX_KIDS);
		} catch (CorruptIndexException e) {

			e.printStackTrace();
		} catch (LockObtainFailedException e) { // TODO Auto-generated catch
												// block
			e.printStackTrace();
		} catch (IOException e) { // TODO Auto-generated
			e.printStackTrace();
		}
*/
		
		try {
			queryDmoz("poems", util.ir.Constants.DMOZ_INDEX_ALL);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (ParseException e) { // TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) { // TODO Auto-generated
			e.printStackTrace();
		}
		
		System.out.println("KIDS -------------------- ");
		try {
			queryDmoz("poems", util.ir.Constants.DMOZ_INDEX_KIDS);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (ParseException e) { // TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) { // TODO Auto-generated
			e.printStackTrace();
		}
		 

	}

}
